from uuid import UUID

from PyQt5.QtWidgets import QVBoxLayout, QLabel, QDialog, QLineEdit, QComboBox

from data import database, get_cover

class AlbumEditor(QDialog):
    COMBO_BOX_UUID_ROLE = 1

    def __init__(self, uuid: UUID):
        super().__init__(None)
        self.setWindowTitle('Album Editor')

        self.uuid: UUID = uuid

        #region
        layout = QVBoxLayout()

        self.uuid_label = QLabel()
        self.uuid_label.setText('Album UUID')
        layout.addWidget(self.uuid_label)

        self.uuid_view = QLineEdit()
        self.uuid_view.setText(str(self.uuid))
        self.uuid_view.setReadOnly(True)
        layout.addWidget(self.uuid_view)

        self.name_label = QLabel()
        self.name_label.setText('Album Name')
        layout.addWidget(self.name_label)

        self.name_edit = QLineEdit()
        self.name_edit.setPlaceholderText('Enter album name here')
        self.name_edit.setText(database['albums'][self.uuid]['name'])
        self.name_edit.editingFinished.connect(self.album_name_changed)
        layout.addWidget(self.name_edit)

        self.artist_label = QLabel()
        self.artist_label.setText('Album Artist')
        layout.addWidget(self.artist_label)

        self.artist_edit = QComboBox()
        self.artist_edit.activated.connect(self.album_artist_changed)
        layout.addWidget(self.artist_edit)
        self.refresh_artist_edit()

        self.cover_label = QLabel()
        self.cover_label.setText('Album Cover')
        layout.addWidget(self.cover_label)

        self.cover_edit = QComboBox()
        self.cover_edit.activated.connect(self.album_cover_changed)
        layout.addWidget(self.cover_edit)

        self.cover_preview_label = QLabel()
        self.cover_preview_label.setDisabled(True)
        layout.addWidget(self.cover_preview_label)
        self.refresh_cover_edit()

        self.setLayout(layout)
        #endregion

    def album_name_changed(self):
        text = self.name_edit.text()
        database['albums'][self.uuid]['name'] = text

    def refresh_artist_edit(self):
        self.artist_edit.clear()
        self.artist_edit.addItem('Unknown Artist')
        current_index = 0
        for uuid in sorted(database['artists'], key=lambda x: str(database['artists'][x]['name']) if database['artists'][x]['name'] else 'Unknown'):
            self.artist_edit.addItem(database['artists'][uuid]['name'] if database['artists'][uuid]['name'] else uuid)
            index = self.artist_edit.count() - 1
            self.artist_edit.setItemData(index, uuid, self.COMBO_BOX_UUID_ROLE)
            if uuid == database['albums'][self.uuid]['artist']:
                current_index = index
        self.artist_edit.setCurrentIndex(current_index)

    def album_artist_changed(self):
        uuid = self.artist_edit.currentData(self.COMBO_BOX_UUID_ROLE)
        if uuid:
            database['albums'][self.uuid]['artist'] = UUID(uuid) if not isinstance(uuid, UUID) else uuid
        else:
            database['albums'][self.uuid]['artist'] = None

    def refresh_cover_edit(self):
        self.cover_edit.clear()
        self.cover_edit.addItem('No Cover')
        current_index = 0
        for uuid in sorted(database['covers'], key=lambda x: str(database['covers'][x]['name']) if database['covers'][x]['name'] else str(uuid)):
            self.cover_edit.addItem(database['covers'][uuid]['name'] if database['covers'][uuid]['name'] else str(uuid))
            index = self.cover_edit.count() - 1
            self.cover_edit.setItemData(index, uuid, self.COMBO_BOX_UUID_ROLE)
            if uuid == database['albums'][self.uuid]['cover']:
                current_index = index
        self.cover_edit.setCurrentIndex(current_index)
        self.album_cover_changed()

    def album_cover_changed(self):
        uuid = self.cover_edit.currentData(self.COMBO_BOX_UUID_ROLE)
        if uuid:
            database['albums'][self.uuid]['cover'] = UUID(uuid) if not isinstance(uuid, UUID) else uuid
            self.cover_preview_label.setEnabled(True)
            cover_label_pixmap = get_cover(uuid)
            cover_label_pixmap = cover_label_pixmap.scaledToWidth(256)
            self.cover_preview_label.setPixmap(cover_label_pixmap)
        else:
            database['albums'][self.uuid]['cover'] = None
            self.cover_preview_label.setEnabled(False)
